//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet.Data
{
    public class QueryTableField : IOfficeXmlSerializable
    {
        private bool _clipped;
        private bool _dataBound = true;
        private ExtensionElement _extension;
        private bool _fillFormulas;
        private int _id;
        private string _name;
        private bool _rowNumbers;
        private TableColumn _tableColumn;

        public TableColumn TableColumn
        {
            get { return _tableColumn; }
            set { _tableColumn = value; }
        }

        public ExtensionElement Extension
        {
            get { return _extension; }
            set { _extension = value; }
        }


        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public bool DataBound
        {
            get { return _dataBound; }
            set { _dataBound = value; }
        }

        public bool RowNumbers
        {
            get { return _rowNumbers; }
            set { _rowNumbers = value; }
        }

        public bool FillFormulas
        {
            get { return _fillFormulas; }
            set { _fillFormulas = value; }
        }

        public bool Clipped
        {
            get { return _clipped; }
            set { _clipped = value; }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("queryTableField"))
            {
                Id = c.GetUIntegerAsInteger("id");
                Name = c.GetOptionalString("name");
                DataBound = c.GetOptionalBoolean("dataBound", true);
                RowNumbers = c.GetOptionalBoolean("rowNumbers", false);
                FillFormulas = c.GetOptionalBoolean("fillFormulas", false);
                Clipped = c.GetOptionalBoolean("clipped", false);
                int tableColumnId = c.GetOptionalUIntegerAsInteger("tableColumnId", 0);
                if (tableColumnId != 0)
                {
                    var table = c.GetData<Table>();
                    Contract.Assert(table != null);
                    TableColumn = Enumerable.First(table.Columns, cc => cc.Id == tableColumnId);
                }
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("queryTableField"))
            {
                c.SetUIntegerAsInteger("id", Id);
                c.SetOptionalString("name", Name);
                c.SetOptionalBoolean("dataBound", DataBound, true);
                c.SetOptionalBoolean("rowNumbers", RowNumbers, false);
                c.SetOptionalBoolean("fillFormulas", FillFormulas, false);
                c.SetOptionalBoolean("clipped", Clipped, false);
                if (TableColumn != null)
                    c.SetOptionalUIntegerAsInteger("tableColumnId", TableColumn.Id, 0);
                c.WriteOptionalElement(Extension);
            }
        }

        #endregion

        /*private int _tableColumnId; //\\hide

        public int TableColumnId
        {
            get { return _tableColumnId; }
            set { _tableColumnId = value; }
        }
         * */
    }
}